<?php
class cls_serve_cart extends cls_serve_base
{
	var $cart;
	function __construct()
	{
		$this->cart=$_SESSION['cart'];
	}	
	function get_cart_num()
	{
		$cart_a=$this->cart;
		$cart_num=0;
		for($i=0;!empty($this->cart[$i]);$i++)
		{
		$cart_num++;
		}
		return $cart_num;
	}
	
	function cart_add($product_id,$num)
	{
		$cart_num=$this->get_cart_num();
		$current_price=$this->get_current_price($product_id);
		if($this->cart_exist($product_id))
		{
			for($i=0;!empty($this->cart[$i]);$i++)
			{
				$cart_a=$this->cart[$i];
			if($product_id==$cart_a['id'])
				$cart_a['num']=$cart_a['num']+$num;

				$this->cart[$i]=$cart_a;
			}
		}
		else 
		{
			$current_price=$this->get_current_price($product_id);
			$cart_b=array(
					'id'=>$product_id,
					'num'=>$num,
					'price'=>$current_price);
			//$cart_b[0]=$product_id;
			//$cart_b[1]=$num;
			$this->cart[$cart_num]=$cart_b;		
		}
		$_SESSION['cart']=$this->cart;
	}
	
	function cart_exist($product_id)
	{
		for($i=0;!empty($this->cart[$i]);$i++)
		{
			$cart_a=$this->cart[$i];
			if($product_id==$cart_a['id'])
				return true;
		}
		return false;
	}
	
	function cart_num_add($id)
	{
		$this->cart[$id]['num']=$this->cart[$id]['num']+1;
		$_SESSION['cart']=$this->cart;
		$cart_tmp=$_SESSION['cart'];
		$num=$cart_tmp[$id]['num'];
		echo json_encode($num);
	}
	
	
	function cart_num_minus($id)
	{
		if($this->cart[$id]['num']==1)
		{
			$this->cart_delete($id);	
		}
		else
		{
		$this->cart[$id]['num']=$this->cart[$id]['num']-1;
		$_SESSION['cart']=$this->cart;
		$cart_tmp=$_SESSION['cart'];
		$num=$cart_tmp[$id]['num'];
		echo json_encode($num);
		}
	}
	
	function cart_delete($id)
	{
		$cart_num=$this->get_cart_num();
		for($i=$id;$i<$cart_num-1;$i++)
		{
			$this->cart[$i]=$this->cart[$i+1];
		}
		unset($this->cart[$cart_num-1]);
		$_SESSION['cart']=$this->cart;
		$msg=0;
		echo json_encode($msg);
		//$_SESSION['cart']=ksort($this->cart);
	}
	
	function cart_empty()
	{
		$this->cart=array();
		$_SESSION['cart']=$this->cart;
	}
	function get_current_price($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$discount_info=$product_data->get_product_discount($product_id);
		if ($discount_info['DiscountType']==1&&$discount_info['Status']==1)
		{
			$current_price=$product_info['Price']*$discount_info['Amount'];
			return $current_price;
		}
		if ($discount_info['DiscountType']==2&&$discount_info['Status']==1)
		{
			$current_price=$product_info['Price']-$discount_info['Amount'];
			return $current_price;
		}
		else
			return $product_info['Price'];
	}
	
	function get_product_name($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		return $product_info['Name'];
	}
	
	function get_product_author($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		return $product_info['Author'];
	}
	
	function get_product_image($product_id)	
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		return $product_info['IMAGE'];
	}
	
	function get_product_quantity($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		return $product_info['quantity'];
	}
	
	function get_saved_proportion($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$saved_proportion=10*$this->get_current_price($product_id)/$product_info['Price'];
		return $saved_proportion;
	}
	
	function get_saved_value($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$saved_value=$product_info['Price']-$this->get_current_price($product_id);
		return $saved_value;
	}
	

}